package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.cache.util.RedisKeys;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
    public static void addCookie(HttpServletResponse response, String name, String value, int timeout) {
        Cookie cookie = new Cookie(name,value);
        //设置有效值
        cookie.setMaxAge(timeout);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    public static String getToken(HttpServletRequest request,HttpServletResponse response){
        String token = null;
        Cookie[] cookies = request.getCookies();
        if(cookies != null && cookies.length > 0){
            for (Cookie cookie : cookies) {
                if ("token".equals(cookie.getName())){
                    token = cookie.getValue();
                    cookie.setPath("/");
                    cookie.setMaxAge(RedisKeys.USER_INFO_TOKEN.getTime().intValue());
                    response.addCookie(cookie);
                    break;
                }
            }
        }
        return token;
    }
}
